import { useEffect } from "react";
import { useRouter, useSegments } from "expo-router";
import { useSelector } from "react-redux";
import { RootState } from "@/stores";

export function RouteGuard({ children }: { children: React.ReactNode }) {
  const router = useRouter();
  const { token } = useSelector((state: RootState) => state.user);
  const segments = useSegments();

  // useEffect(() => {
  //   try {
  //     const currentPath = `/${segments.join("/")}`;
  //     const isAuthPage = currentPath.includes("/(views)/auth");

  //     if (!token && !isAuthPage) {
  //       router.replace("/(views)/auth/login");
  //     }
  //   } catch (error) {
  //     // 发生错误时重定向到登录页
  //     router.replace("/(views)/auth/login");
  //   }
  // }, [segments, token]);

  return <>{children}</>;
}

export default RouteGuard;
